package cn.jiguang.demo;

import android.util.Log;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class CryptoUtils {
    private static final String ALGORITHM = "AES";
    private static final String TAG = "CryptoUtils";

    public static void encrypt(String key, File inputFile, File outputFile) {
        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
    }

    public static void decrypt(String key, File inputFile, File outputFile) {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }

    private static void doCrypto(int cipherMode, String key, File inputFile,
                                 File outputFile) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            System.out.println("doCrypto cipherMode is " + cipherMode + ", key is " + key +
                    ", inputFile is " + inputFile.getAbsolutePath() +
                    ", outputFile is " + outputFile.getAbsolutePath());
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(cipherMode, secretKey);


            inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            byte[] outputBytes = cipher.doFinal(inputBytes);

            outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);
            outputStream.flush();
        } catch (NoSuchPaddingException | NoSuchAlgorithmException
                | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException | IOException ex) {
            System.out.println("Error encrypting/decrypting file, error message is " + ex.getMessage());
        } finally {
            closeQuietly(inputStream);
            closeQuietly(outputStream);
        }
    }

    public static void closeQuietly(Closeable closeable){
        if (closeable != null){
            try {
                closeable.close();
            } catch (Throwable e) {}
        }
    }

    public static void main(String[] args) {
        try {
            File aesFile = new File("/Users/an/Downloads/android-JPush_Android-54518846781.aes");
            File logFile = new File("/Users/an/Downloads/testlog.txt");
            if (!logFile.exists()) {
                logFile.createNewFile();
            }
            CryptoUtils.decrypt("8f02a4fa717a6235734d92de", aesFile, logFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
